package com.drcoffee.drcoffeemall.service.impl;

import com.drcoffee.drcoffeemall.entry.InvoiceRecords;
import com.drcoffee.drcoffeemall.entry.Result;
import com.drcoffee.drcoffeemall.mapper.FilePathMapper;
import com.drcoffee.drcoffeemall.mapper.InvoiceMapper;
import com.drcoffee.drcoffeemall.mapper.OrderMapper;
import com.drcoffee.drcoffeemall.service.InvoiceService;
import com.drcoffee.drcoffeemall.util.OSSUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class InvoiceServiceImpl implements InvoiceService {


    final
    RedisTemplate<String, Object> redisTemplate;

    final
    InvoiceMapper invoiceMapper;

    final
    OrderMapper orderMapper;

    final
    FilePathMapper filePathMapper;

    final
    OSSUtil ossUtil;


    public InvoiceServiceImpl(InvoiceMapper invoiceMapper, RedisTemplate<String, Object> redisTemplate, OrderMapper orderMapper, FilePathMapper filePathMapper, OSSUtil ossUtil) {
        this.redisTemplate = redisTemplate;
        this.invoiceMapper = invoiceMapper;
        this.orderMapper = orderMapper;
        this.filePathMapper = filePathMapper;
        this.ossUtil = ossUtil;
    }


    @Override
    public ResponseEntity<Object> getInvoiceData(@RequestBody Map<String, Object> paramMap) {
        Integer currPage = (Integer) paramMap.get("currPage");
        Integer pageSize = (Integer) paramMap.get("pageSize");
        PageHelper.startPage(currPage, pageSize);
        List<Map<String, Object>> invoice_list = invoiceMapper.getInvoiceData(paramMap);
        List<Object> getNot = orderMapper.getNot();
//        List<Map<String, Object>> filteredInvoiceList = invoice_list.stream()
//                .filter(invoice -> !getNot.contains(invoice.get("order_code")))
//                .collect(Collectors.toList());
        List<Map<String, Object>> filteredInvoiceList = invoice_list.stream()
                .filter(invoice -> !getNot.stream().anyMatch(order -> order.equals(invoice.get("order_code"))))
                .collect(Collectors.toList());
        PageInfo<Map<String, Object>> originalPageInfo  = new PageInfo<>(invoice_list);
        PageInfo<Map<String, Object>> resultData = new PageInfo<>();
        resultData.setList(filteredInvoiceList);
        resultData.setTotal(originalPageInfo.getTotal());
        resultData.setPageNum(originalPageInfo.getPageNum());
        resultData.setPageSize(originalPageInfo.getPageSize());
        return new ResponseEntity<>(resultData, HttpStatus.OK);
    }

    @Override
    public Result uploadInvoice(MultipartFile file, String code, String order_code) {
        // 获取发票上传路径
        String uploadPath = filePathMapper.getUploadPath("L", 1);
        Result uploadResult = ossUtil.upload(file, uploadPath, code + ".pdf");
        if (uploadResult.getResult() == 1) {
            // 文件上传成功
            invoiceMapper.setInvoicePath(code, (String) uploadResult.getObject());
            // 设置订单开票状态
            orderMapper.setInvoiceStatus(order_code, 2);
        }
        return uploadResult;
    }

    @Override
    public HashMap<Object,Integer> updateCurrentStep(String orderCode) {
        Integer num = invoiceMapper.num(orderCode);
        Integer cc;
        if (num==2){
            cc=0;
            HashMap<Object,Integer> map1  = new HashMap<>();
            map1.put("get",cc);
            return map1;
        }
        Integer ss = invoiceMapper.updateCurrentStep(orderCode);
        HashMap<Object,Integer> map  = new HashMap<>();
        map.put("get",ss);
        return map;
    }

}
